package classloader;

import org.junit.Test;

/**
 * @ClassName Demo01
 * @Description TODO
 * 类加载器 测试
 * @Author liang
 * @Date 2018\10\22 0022 15:21
 * @Version 1.0
 **/
public class Demo01 {
    @Test
    public void test01() throws ClassNotFoundException {

        FileSystemClassLoader loader = new FileSystemClassLoader("D:/");
        FileSystemClassLoader loader2 = new FileSystemClassLoader("D:/");


        Class<?> c1 = loader.loadClass("Hi");
        Class<?> c12 = loader.loadClass("Hi");
        Class<?> c2 = loader2.loadClass("Hi");

        System.out.println(c1.hashCode()+"==="+c12.hashCode());//同一个类被同一个类加载器加载，jvm认为是同一个类
        System.out.println(c1.hashCode()+"==="+c2.hashCode());//同一个类被不同的类加载器加载，jvm认为是不同的类
        System.out.println(c1.getClassLoader()+"====="+c12.getClassLoader()+"===="+c2.getClassLoader());

    }

    /***
     * 本项目下的class文件，已经被appClassLoader装载了
     *
     */
    @Test
    public void test02() throws ClassNotFoundException {
        FileSystemClassLoader loader1 = new FileSystemClassLoader("D:/a_code/demo/datasendClient/target/classes");

        FileSystemClassLoader loader2 = new FileSystemClassLoader("D:/a_code/demo/datasendClient/target/classes");
        Class<?> c1 = loader1.loadClass("com.joymeter.Application");
        Class<?> c2 = loader2.loadClass("com.joymeter.Application");

        System.out.println(c1.hashCode()+"====="+c2.hashCode());
        System.out.println(c1.getClassLoader()+"====="+c2.getClassLoader());


    }

    /**
     * 其他项目路径下的是被自定义类加载器加载
     * D:\a_code\demo\mySpring\target\classes\designPattern\pay
     */
    @Test
    public void test03() throws ClassNotFoundException {
        FileSystemClassLoader loader1 = new FileSystemClassLoader("D:/a_code/demo/mySpring/target/classes");

        FileSystemClassLoader loader2 = new FileSystemClassLoader("D:/a_code/demo/mySpring/target/classes");
        Class<?> c1 = loader1.loadClass("designPattern.pay.pay");
        Class<?> c2 = loader2.loadClass("designPattern.pay.pay");

        System.out.println(c1.hashCode()+"====="+c2.hashCode());
        System.out.println(c1.getClassLoader()+"====="+c2.getClassLoader());
    }

    /**
     * 加载加密了的class
     * @throws ClassNotFoundException
     */
    @Test
    public void test04() throws ClassNotFoundException {

        DecryptClassLoader loader = new DecryptClassLoader("D:/encryptionClass");


        Class<?> c1 = loader.loadClass("Hi");



        System.out.println(c1.getClassLoader());

    }
    
}
